package com.resys.server.controller.admin;

import com.resys.entity.dto.StatisticDto;
import com.resys.entity.vo.ResponseVO;
import com.resys.entity.vo.SalesReportVo;
import com.resys.entity.vo.StatisticLogVo;
import com.resys.server.controller.ABaseController;
import com.resys.service.CustomerService;
import com.resys.service.UserService;
import com.resys.service.impl.CustomerServiceImpl;
import com.resys.service.impl.ReservationorderServiceImpl;
import com.resys.service.impl.RoomstatuslogServiceImpl;
import com.sun.org.apache.bcel.internal.generic.RETURN;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController("StatisticsController")
@Api(tags = "管理员统计接口")
@RequestMapping("/admin/statistics")
public class StatisticsController extends ABaseController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private RoomstatuslogServiceImpl roomstatuslogService;
    @Autowired
    private ReservationorderServiceImpl reservationorderService;

    @ApiOperation("统计客户端网站访问流量")
    @GetMapping("/client")
    public ResponseVO client(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate startDate, @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate endDate) {
        List<Integer> list = new ArrayList<>();
        LocalDate startcopyDate = startDate;
        while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
            LocalDateTime start = startDate.atTime(0, 0, 0);
            LocalDateTime end = startDate.atTime(23, 59, 59);
            list.add(customerService.getCountLogin(start, end));
            startDate = startDate.plusDays(1);
        }
        List<StatisticDto> result = new ArrayList<>();
        Integer i = 0;
        while(startcopyDate.isBefore(endDate) || startcopyDate.isEqual(endDate)) {
            StatisticDto statisticDto = new StatisticDto();
            statisticDto.setDate(startcopyDate);
            statisticDto.setCount(list.get(i));
            i++;
            startcopyDate = startcopyDate.plusDays(1);
            result.add(statisticDto);
        }
        return getSuccessResponseVO(result);

     }
     @ApiOperation("统计某个员工工作日志")
     @GetMapping("/getLog")
     public ResponseVO getLog(String userId,@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate startDate, @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate endDate){
         List<StatisticLogVo> list = new ArrayList<>();
         while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
             StatisticLogVo statisticLogVo = new StatisticLogVo();
             LocalDateTime start = startDate.atTime(0, 0, 0);
             LocalDateTime end = startDate.atTime(23, 59, 59);
             statisticLogVo.setDay(startDate);
             statisticLogVo.setWorkCount(roomstatuslogService.getCountlog(userId,start,end));
             list.add(statisticLogVo);
             startDate = startDate.plusDays(1);
         }
        return getSuccessResponseVO(list);
     }
     @ApiOperation("统计营业额数据包括完单率每一天")
     @GetMapping
     public ResponseVO statistics(@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate startDate,@DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate endDate){
        List<SalesReportVo> list =new ArrayList<>();
         while (startDate.isBefore(endDate) || startDate.isEqual(endDate)) {
             SalesReportVo vo= new SalesReportVo();
             LocalDateTime start = startDate.atTime(0, 0, 0);
             LocalDateTime end = startDate.atTime(23, 59, 59);
             vo.setDay(startDate);
             vo.setSales(reservationorderService.getSales(start,end));
             vo.setCompleteRate(reservationorderService.getRate(start,end));
             list.add(vo);
             startDate = startDate.plusDays(1);
         }

        return  getSuccessResponseVO(list);
     }
     @ApiOperation("导出excel营业额数据")
     @GetMapping("/export")
     public void export(HttpServletResponse response){
         reservationorderService.export(response);

     }

}
